#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int pro(vector<vector<int>>& rec, int x, int y, int now)
{
    if ((x >= rec.size()) || (x < 0) || (y >= rec[0].size()) || (y < 0)) {
	return INT_MAX;
    }
    if (rec[x][y] == 0) {
	return now;
    }
    int a=pro(rec,x+1,y,now+1);
    int b=pro(rec,x-1,y,now+1);
    int c=pro(rec,x,y+1,now+1);
    int d=pro(rec,x,y-1,now+1);
    return min(a,min(b,min(c,d)));
}


vector<vector<int>> updateMatrix(vector<vector<int>>& matrix)
{
	vector<vector<int>> res;
	res.resize(matrix.size());
	for(int i=0;i<matrix.size();++i){
		res[i].resize(matrix[0].size());
	}
	for(int i=0;i<matrix.size();++i){
		for(int j=0;j<matrix[0].size();++j){
			res[i][j]=pro(matrix,i,j,0);
		}
	}
	return res;
}

int main(int argc, char* argv[])
{
    vector<vector<int>> a={{0,0,0},{0,1,0},{1,1,1}};
    auto p = updateMatrix(a);
    for(int i=0;i<p.size();++i){
	for(int j=0;j<p[0].size();++j){
		cout<<p[i][j]<<" ";
	}
	cout<<endl;
    }
    return 0;
}
